package sms

import (
	"fmt"
	"log"

	"github.com/aliyun/alibaba-cloud-sdk-go/services/dysmsapi"
)

// 阿里云短信 SDK 初始化配置
func SendSms(phone string, code string) error {
	// 使用阿里云 AccessKeyId 和 AccessKeySecret
	client, err := dysmsapi.NewClientWithAccessKey("cn-hangzhou", "<YourAccessKeyId>", "<YourAccessKeySecret>")
	if err != nil {
		log.Printf("Error creating SMS client: %v", err)
		return err
	}

	request := dysmsapi.CreateSendSmsRequest()
	request.SetScheme("https")
	request.PhoneNumbers = phone
	request.SignName = "<YourSignName>"                        // 短信签名
	request.TemplateCode = "<YourTemplateCode>"                // 短信模板代码
	request.TemplateParam = fmt.Sprintf(`{"code":"%s"}`, code) // 短信模板中的验证码

	// 发送短信
	response, err := client.SendSms(request)
	if err != nil {
		log.Printf("Error sending SMS: %v", err)
		return err
	}

	if response.Code != "OK" {
		log.Printf("Error response from SMS service: %s", response.Message)
		return fmt.Errorf("短信发送失败")
	}

	return nil
}
